<!--
 * @Date: 2023-03-14 14:28:15
 * @LastEditTime: 2023-04-03 20:29:44
 * @FilePath: /NLK/src/views/home/home.vue
 * 介绍:首页
-->
<script lang="ts" setup>
import { addCache } from "@@/hooks/routeCache";
import { _require } from "@@/utils/tools/tools";
import userinfo from "./components/userinfo.vue";
import coininfo from "./components/coininfo.vue";
import coinList from "./components/coinList.vue";
import PubSub from "pubsub-js";
import {
  apiHomeData,
  HomeData,
  apiStoreList,
  StoreList,
} from "@@/api/page/home";
import type CpageView from "@@/components/common/CpageView/CpageView.vue";

const CpageViewRef = ref<InstanceType<typeof CpageView>>();

/**首页数据 */
const homeData = ref<HomeData.Res>();
const storeList = ref<StoreList.Res>();
/**获取首页数据 */
async function getHomeData() {
  CpageViewRef.value?.pullRefreshStart();
  const [homeRes, storeRes] = await Promise.all([
    apiHomeData(),
    apiStoreList(),
  ]);
  if (!homeRes || !storeRes) return CpageViewRef.value?.pullRefreshFail();
  homeData.value = homeRes;
  storeList.value = storeRes;
  CpageViewRef.value?.pullRefreshSuccess();
}

if (window.isLogined) getHomeData();
else PubSub.subscribeOnce("logined", getHomeData);
addCache("home");
</script>

<template>
  <CpageView
    ref="CpageViewRef"
    @refresh="getHomeData"
    pull-refresh
    class="home C-T5"
  >
    <BtabbarNav title="Neuralink" />
    <userinfo :homeData="homeData" />
    <div class="contentCard auto-MT-sm C-T1">
      <coininfo
        :coins="Object.values(homeData?.coins || {})"
        :last-airdrop-time="homeData?.last_time || '2023-04-02 08:00:00'"
      />
      <coinList
        :store="Object.values(storeList?.storage || {})"
        :to-address="storeList?.address || ''"
      />
    </div>
  </CpageView>
</template>

<style lang="less" scoped>
.contentCard {
  background-color: var(--C-B2);
  border-radius: var(--R-md) var(--R-md) 0 0;
  min-height: calc(var(--page-H) - 625rem);
}
.home {
  padding: 0 !important;
  background-color: var(--C-T1);
}
</style>
